//
//  FundsLimitController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/23.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "FundsLimitController.h"
#import "UserPaySettingController.h"

#import "FoundsLimitCell.h"


#import "UserPaySettingController.h"

@interface FundsLimitController ()

@property(nonatomic,strong)NSArray *limit;

@property (nonatomic, strong) NSIndexPath *selectedPath;



@end

@implementation FundsLimitController
- (void)returnText:(ReturnTextBlock)returnBlock {
    self.returnBlock = returnBlock;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    if (self.returnBlock != nil && _selectedLimit != nil) {
        
        self.returnBlock(self.selectedLimit);
    }
    else
    {
        NSLog(@"nishishaca");
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(NSArray *)limit
{
    if (!_limit) {
        _limit = @[@"200",@"500",@"800",@"1000",@"2000"];
    }
    return _limit;
}
-(void)setupUI
{
    self.title = @"默认支付的银行卡";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  
    [self setupNavBackButton];
    
    [self createNavigationBarRightBarButtonItemWithTitle:@"取消"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FoundsLimitCell" bundle:nil] forCellReuseIdentifier:@"limit"];
    
    
}
#pragma mark - Table View

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  kNormalCellHeight;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.limit count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoundsLimitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"limit"];

    cell.limitLabel.text = [NSString stringWithFormat:@"%@ 元／笔",[self.limit objectAtIndex:indexPath.row]];
    
    NSLog(@"%@",self.selectedLimit);
    cell.accessoryType = [self.selectedLimit isEqualToString:self.limit[indexPath.row]] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // NSLog(@"!!!!!!!!!%@",cell.textLabel.text);

    self.selectedLimit = self.limit[indexPath.row];


    [self.navigationController popViewControllerAnimated:YES];
    if ([self.navigationController.viewControllers[1] isKindOfClass:[UserPaySettingController class]])
    {
        UserPaySettingController *controller =(UserPaySettingController *) self.navigationController.viewControllers[1];
        
        if ([self.delegate respondsToSelector:@selector(changePayLimit:)]) {
            if (![self.selectedLimit isEqualToString:controller.selectedLimit]) {
                [self.delegate changePayLimit:YES];
                controller.selectedLimit = self.selectedLimit;
            }else{
                [self.delegate changePayLimit:NO];
            }
        }
        
        
        
        [self.navigationController popToViewController:controller animated:YES];
    }

    
}

-(void)rightAction
{
    [self goBackAction];
}
- (void)goBackAction
{
    
    [self.delegate changePayLimit:NO];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
